package com.antcom.antread.common.core.serialize;

import com.alibaba.fastjson.JSONObject;
import com.antcom.antread.common.core.exception.CustomException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * json 序列化,反序列化
 *
 * @Author 中国蚂蚁技术有限公司
 * @create 2020/12/26 9:08
 */
public class JSONSerialize {

    /**
     * jackson 反序列化
     *
     * @param param 参数
     * @param clazz 类型
     * @param <T>   泛型
     * @return
     */
    public static <T> T deserializeJackSon(String param, Class<T> clazz) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            return objectMapper.readValue(param, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException(e.getMessage());
        }
    }

    /**
     * fastJson 反序列化
     *
     * @param param
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T deserializeFastJson(String param, Class<T> clazz) {
        T object = JSONObject.parseObject(param, clazz);
        return object;
    }
}
